package sg.edu.nus.iss.cats.action;

import javax.servlet.http.*;



/** WORKSHOP W06_CONTROLLER
  * Import struts action classes
  *********/
import org.apache.struts.action.*;
import org.apache.commons.beanutils.*;
import sg.edu.nus.iss.cats.form.*;
import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.exception.*;


/** WORKSHOP W06_CONTROLLER
  *  Extend from the org.apache.struts.actions.Action class
  *********/
public class CourseUpdateAction extends Action {

    /**
      * The execute method receives the course details from the
      * courseForm, creates a Course object
      * from the form bean and sends it for update in the DB
      */
      
     /** WORKSHOP W06_CONTROLLER
      * Throw the CourseNotFoundException,CourseUpdateException
      * to handle in struts-config.xml as a global exception
      *********/
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
                         throws CourseNotFoundException, CourseUpdateException {

        System.out.println("CourseUpdateAction: execute()");
        
         /** WORKSHOP W06_CONTROLLER
          * Get employeeId of the person updating this course application, from
          * the HttpSession.
          * Use the session.getAttribute("USERSESSION") method to get UserSession
          * and then the UserSession.getEmployee().getEmployeeId() method to get
          * the employee id of this user
          *********/
        HttpSession session = request.getSession();
        UserSession userSession =
            (UserSession) session.getAttribute("USERSESSION");
        String modifierId = userSession.getEmployee().getEmployeeId();
        

        /** WORKSHOP W06_CONTROLLER
          * Create a blank Course, and initialize it with data
          * from the form bean
        *********/
        CourseForm caForm = (CourseForm) form;
        Course courseApplication = new Course();
        try {
            BeanUtils.copyProperties(courseApplication, caForm);
        } catch (Exception e) {
            System.out.println("Error converting from form bean: " + e);
            throw new CourseUpdateException(
                "Could not create the Course Application: " + e);
        }

        
        /** WORKSHOP W06_CONTROLLER
          * Set the course status to UPDATED. Use Course.UPDATED
        *********/
        courseApplication.setStatus(Course.UPDATED);
        
        
        /** WORKSHOP W06_CONTROLLER
        * Update the course application
        * Use the updateApplication() method of the CourseManager
        *********/

        CourseManager caBD = new CourseManager();
        caBD.updateCourse(courseApplication, modifierId, "Modified");

        System.out.println("execute() successful");
        
                  
        /** WORKSHOP W06_CONTROLLER
          * return to global forward "employeeDefault" to display the updated course
          * application list
        *********/
        //throw new CourseNotFoundException("soehanwin couse not found");
        return mapping.findForward("employeeDefault");
    }

}